package edu.neusoft.yiqibike.common.entity.convert;

import edu.neusoft.yiqibike.common.entity.mysql.Role;
import edu.neusoft.yiqibike.common.entity.vo.AuthoRangeVo;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * User：lyc
 * Date：2017-12-31
 * Time：15:54
 * Description：
 */
public class RoleRangeConverter extends Converter<AuthoRangeVo, Role> {

    public RoleRangeConverter(final Integer level, final List<String> roleCodes) {
        super(role -> {
            AuthoRangeVo roleRangeVo = new AuthoRangeVo();
            roleRangeVo.setKey(role.getId());
            roleRangeVo.setLabel(role.getCode());
            roleRangeVo.setType("role");
            roleRangeVo.setPinyin(role.getCode());
            if (Objects.nonNull(level)) {
                boolean isAllow = true;
                if (Integer.compare(level, role.getLevel()) < 0) {
                    //如果管理员级别小于当前角色的级别但是该管理员拥有当前角色则管理员可对当前角色进行操作
                    if (!CollectionUtils.isEmpty(roleCodes) && roleCodes.contains(role.getCode())) {
                        isAllow = false;
                    }
                } else {
                    //如果管理员级别大于当前角色的级别则可对当前角色进行操作
                    isAllow = false;
                }
                roleRangeVo.setDisabled(isAllow);
            }
            return roleRangeVo;
        }, vo -> new Role(vo.getKey(), vo.getLabel()));
    }
}
